package com.shujia.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.*;
import java.sql.*;
import java.util.Properties;

public class Code04Mysql2HDFS {

    public static void main(String[] args) throws ClassNotFoundException, IOException, SQLException {


        //1、注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        // 2.创建连接
        Properties properties = new Properties();
        properties.load(new FileReader("hadoopCode/src/main/resources/mysql.properties"));
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String passwd = properties.getProperty("passwd");
        Connection connection = DriverManager.getConnection(url, user, passwd);


        Statement statement = connection.createStatement();

        ResultSet resultSet = statement.executeQuery("select * from user_accumulate_less_9900");

        FileSystem fileSystem = null;
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://master:9000");
        conf.set("dfs.replication", "2");
        System.setProperty("HADOOP_USER_NAME", "root");
        try {
            fileSystem = FileSystem.get(conf);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/api/user_accumulate_less_9900.txt"));

        // 由字节流转成字符流 做数据的写出
//        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fsDataOutputStream);
//        BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);



        while (resultSet.next()) {
            String line = "" +
                    resultSet.getString(1) + "\t" +
                    resultSet.getString(2) + "\t" +
                    resultSet.getString(3) + "\t" +
                    resultSet.getString(4) + "\t" +
                    resultSet.getString(5) + "\t" +
                    resultSet.getString(6) + "\n";
            fsDataOutputStream.write(line.getBytes());
            // 可以尝试缓存一部分数据以后再做数据的刷写操作
            fsDataOutputStream.flush();
        }

        // 关闭操作
        fsDataOutputStream.close();
        statement.close();
        connection.close();
        fileSystem.close();

    }
}
